import API from '../../api/api.js'
const FORMAT = require("../../api/util.js")
const app = getApp()
import Dialog from '../../lib/vant-weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: 0,
    dataList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.data.screenHeight = app.globalData.screenHeight
    this.setData({
      screenHeight: this.data.screenHeight
    })
    let today = FORMAT.formatDay(new Date().getTime() / 1000)
    API.getShiGuangPingDataByOpenId(userInfo._openid, today).then(res => {
      this.setData({
        dataList: res
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  onClickShiGuangPing: function (e) {
    let item = e.currentTarget.dataset.value
    wx.setStorageSync('currContent', item)
    wx.navigateTo({
      url: "/pages/conentdisplay/conentdisplay",
    })
  },

  onDeleteShiGuangPing: function (e) {
    let item = e.currentTarget.dataset.value
    let index = e.currentTarget.dataset.index
    Dialog.confirm({
        title: '删除时光瓶',
        message: '请谨慎确定是否删除此时光瓶，删除后无法恢复。',
      })
      .then(() => {
        wx.showLoading({
          title: '加载中...',
          mask: true
        })
        API.deleteCollectionItem("shiguangping", item._id).then(res => {
          this.data.dataList.splice(index, 1)
          this.setData({
            dataList: this.data.dataList
          })
          wx.hideLoading()
          wx.showToast({
            title: '删除成功',
            icon: "none"
          })
        })
      })
      .catch(() => {
        wx.showToast({
          title: '已取消删除时光瓶',
          icon: "none"
        })
      })
  },

  onClose: function (e) {
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index],
      path: '/pages/index/index'
    }
  },
  onShareTimeline: function () {
let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index]
    }
  }
})